// function loadMain() {
//     $.ajax({
//         url: 'http://www.xiongmaoyouxuan.com/api/tab/1',
//         type: 'get',
//         headers: {
//             'x-platform': 'pc'
//         },
//         success: function(result) {
//             if (result.code === 200) {
//                 let banners = result.data.banners
//                 console.log(banners);
//                 let productList = result.data.items.list
//                 console.log(productList);
//             }
//         }
//     })
// }
// loadMain()

// function showSwiper(banners) {
//     if (banners.length == 0) {
//         let sliperitem = `<div class="swiper-slide"><img src="${item.imageUrl}" alt=""></div>`
//         $('.swiper-wrapper').html(sliperitem)
//     } else {
//         let newsliperitem = banners.map(item => {
//             return `<div class="swiper-slide"><img src="${item.imageUrl}" alt=""></div>`
//         })
//         let sliperitem = newsliperitem.join('')
//         $('.swiper-wrapper').html(sliperitem)
//     }
// }

const log = document.querySelector('.log')
const index = document.querySelector('.index')
const nine = document.querySelector('.nine')
const coupon = document.querySelector('.coupon')
const ambulance = document.querySelector('.ambulance')
const register = document.querySelector('.register')

const btn = document.querySelector('.choice-more-btn')

$(function() {
    getUserList()


})
let userList = []
let str = ''
let pageNo = 0

function more() {

    $.ajax({
        // url: 'http://www.xiongmaoyouxuan.com/api/tab/1/feeds?start=40&sort=0',
        url: 'http://www.xiongmaoyouxuan.com/api/tab/1/feeds',
        type: 'get',
        data: {
            start: pageNo++ * 20
        },
        dataType: 'json',
        success: function(data) {
            showUserList(data.data.list)
        }
    })

}

function getUserList() {
    $.ajax({
        // url: 'http://www.xiongmaoyouxuan.com/api/tab/7?start=0',
        url: 'http://www.xiongmaoyouxuan.com/api/tab/1',
        type: 'get',
        dataType: 'json',
        success: function(data) {
            showUserList(data.data.items.list)
        }
    })
}

function showUserList(userList) {

    userList.forEach(item => {
        let itemstr = `
        <li class="identify" onclick="productDetail(${item.id})">
        <div class="commodity-card">
        <img src="${item.image}" alt="">
        <div class="msg">
            <div class="title">${item.title}</div>
            <div class="keyword">
                <span class="tmall">天猫</span>
                <span class="postage">包邮</span>
            </div>
            <div class="foot">
                <span class="price-tag">¥</span>
                <span class="price-strong">${item.price}</span>
                <span class="price-digit"></span>
                <span class="sale-num">${item.saleNum}人已买</span>
                <span class="coupon-value">${item.couponValue}</span>
            </div>
        </div>
        </div>
                    </li>
        
        
        
        `
        str += itemstr
        document.querySelector('.allProduct').innerHTML = str
        let num = userList.length % 4
        if (num != 0) {
            userList.splice(userList.length - num)
        }
    });

}


// log.addEventListener('click', function() {
//         location.href = 'try.html'
//     })
// index.addEventListener('click', function() {
//     location.href = 'index.html'
// })
// nine.addEventListener('click', function() {
//     location.href = 'coupon.html'
// })
// coupon.addEventListener('click', function() {
//     location.href = 'postage.html'
// })
// ambulance.addEventListener('click', function() {
//     location.href = 'coolingWear.html'
// })
// register.addEventListener('click', function() {
//     // location.href = 'https://reg.taobao.com/member/reg/fast/union_reg?_regfrom=TB'
//     location.href = 'try.html'
// })

function productDetail(id) {
    // alert(id)
    location.href = 'product.html?id=' + id
}
window.onload = function() {
    var goback = document.querySelector('.goBack')
    var sliderbar = document.querySelector('.return1')
    document.addEventListener('scroll', function() {
        if (window.pageYOffset >= 1200) {
            goback.style.display = 'block'
        } else {
            goback.style.display = 'none'
        }
    })
    goback.addEventListener('click', function() {
        // window.scroll(0, 100)
        animate(window, 0)
    })
}

function animate(obj, target, callback) {
    clearInterval(obj.timer)
    obj.timer = setInterval(function() {
        var step = (target - window.pageYOffset) / 10
        step = step > 0 ? Math.ceil(step) : Math.floor(step)
        if (window.pageYOffset == target) {
            clearInterval(obj.timer)
                // if (callback) {
                //     callback()
                // }
            callback && callback()
        }
        // obj.style.left = obj.offsetLeft + step + 'px'
        window.scroll(0, window.pageYOffset + step)
    }, 15)
}